﻿using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace Repid.Utility.Helper
{
    public static class QrCodeHelper
    {
        public static void GenterateQrCode(string strContent, string path, string filename)
        {
            ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.L; //误差校正水平  
  
            QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域  
 
            int ModuleSize = 12;//大小  

            var encoder = new QrEncoder(Ecl);

            QrCode qr;

            if (encoder.TryEncode(strContent, out qr))//对内容进行编码，并保存生成的矩阵  
            {
                var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));

                using (var fs = new FileStream(path + filename, FileMode.Create, FileAccess.Write))
                {
                    render.WriteToStream(qr.Matrix, ImageFormat.Png, fs);
                }
            }
            else { }
        }

        public static void GenterateQrCode(string strContent, Stream stream)
        {
            ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.L; //误差校正水平  

            QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域  

            int ModuleSize = 12;//大小  

            var encoder = new QrEncoder(Ecl);

            QrCode qr;

            if (encoder.TryEncode(strContent, out qr))//对内容进行编码，并保存生成的矩阵  
            {
                var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));

                render.WriteToStream(qr.Matrix, ImageFormat.Png, stream);
            }
            else { }
        }
    }
}